package auto.app.service.impl;

import auto.app.controller.from.TestingEnvironmentAddFrom;
import auto.app.controller.from.TestingEnvironmentDeleteFrom;
import auto.app.controller.from.TestingEnvironmentPage;
import auto.app.controller.from.TestingEnvironmentUpdateFrom;
import auto.app.controller.info.TestingEnvironmentInfo;
import auto.app.controller.info.TestingEnvironmentPageInfo;
import auto.app.dao.AccountDao;
import auto.app.exetion.MainExection;
import auto.app.model.Account;
import auto.app.model.TestingEnvironment;
import auto.app.dao.TestingEnvironmentDao;
import auto.app.service.TestingEnvironmentService;
import auto.app.util.CommonUtil;
import auto.app.util.UserGetAccount;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static auto.app.exetion.MainExetionMessage.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2022-07-09
 */
@Service
public class TestingEnvironmentServiceImpl extends ServiceImpl<TestingEnvironmentDao, TestingEnvironment> implements TestingEnvironmentService {

    @Autowired
    private AccountDao accountDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addTestingEnvironment(TestingEnvironmentAddFrom testingEnvironmentAddFrom) throws MainExection {
        //获得TestingEnvironment对象
        TestingEnvironment testingEnvironment = CommonUtil.copyOne(testingEnvironmentAddFrom, TestingEnvironment.class);
        // 获取创建人相关信息
        testingEnvironment.setCreateAccountId(UserGetAccount.getAccountId());
        Account account = accountDao.selectById(testingEnvironment.getCreateAccountId());
        if (account == null) throw new MainExection(ACCOUNTSELECTONEERROR.getCode(), ACCOUNTSELECTONEERROR.getMsg());
        testingEnvironment.setCreateAccountName(account.getAccountName());
        testingEnvironment.setCreateTime(LocalDateTime.now());
        //传入数据库
        int insert = baseMapper.insert(testingEnvironment);
        return insert > 0;
    }

    //列表接口
    @Override
    public TestingEnvironmentPageInfo listTestingEnvironment(TestingEnvironmentPage testingEnvironmentPage) {
        String apiname = testingEnvironmentPage.getApiname();
        QueryWrapper<TestingEnvironment> queryWrapper = new QueryWrapper<>();
        if (apiname != null && !apiname.equals("")) {
            queryWrapper.like("apiname", testingEnvironmentPage.getApiname());
        }
        queryWrapper.orderByDesc("create_time");
        TestingEnvironmentPage testingEnvironmentPage1 = baseMapper.selectPage(testingEnvironmentPage, queryWrapper);
        //转换成视图类
        List<TestingEnvironment> records = testingEnvironmentPage1.getRecords();
        List<TestingEnvironmentInfo> testingEnvironmentInfos = CommonUtil.copyList(records, TestingEnvironmentInfo.class);
        //page类赋值返回给前端
        TestingEnvironmentPageInfo testingEnvironmentPageInfo = CommonUtil.copyOne(testingEnvironmentPage1, TestingEnvironmentPageInfo.class);
        testingEnvironmentPageInfo.setRecords(testingEnvironmentInfos);
        return testingEnvironmentPageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateTestingEnvironment(TestingEnvironmentUpdateFrom testingEnvironmentUpdateFrom) throws MainExection {
        //查询出对应的数据
        TestingEnvironment testingEnvironment = baseMapper.selectById(testingEnvironmentUpdateFrom.getDatasourceId());
        if (testingEnvironment == null)
            throw new MainExection(TESTINGENVIRONMENT.getCode(), TESTINGENVIRONMENT.getMsg());
        //进行修改
        LambdaUpdateWrapper<TestingEnvironment> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(TestingEnvironment::getDatasourceId,testingEnvironmentUpdateFrom.getDatasourceId())
                .set(TestingEnvironment::getApiname,testingEnvironmentUpdateFrom.getApiname())
                .set(TestingEnvironment::getIp,testingEnvironmentUpdateFrom.getIp())
                .set(TestingEnvironment::getPort,testingEnvironmentUpdateFrom.getPort())
                .set(TestingEnvironment::getAgreement,testingEnvironmentUpdateFrom.getAgreement())
                .set(TestingEnvironment::getState,testingEnvironmentUpdateFrom.getState())
                .set(TestingEnvironment::getRemark,testingEnvironmentUpdateFrom.getRemark())
                .set(TestingEnvironment::getLastupdateTime,LocalDateTime.now())
                .set(TestingEnvironment::getLastupdateAccountId,UserGetAccount.getAccountId())
                .set(TestingEnvironment::getCreateAccountName,UserGetAccount.getAccountName());
        return this.update(lambdaUpdateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteids(TestingEnvironmentDeleteFrom testingEnvironmentDeleteFrom) {
        //先查询集合中的id是否存在
        List<String> ids = testingEnvironmentDeleteFrom.getIds();
        ArrayList<String> integerArrayList = new ArrayList<>();
        //对集合中数据遍历处理
        ids.stream().distinct().forEach(integer -> {
            TestingEnvironment testingEnvironment = baseMapper.selectById(integer);
            if (testingEnvironment == null) try {
                throw new MainExection(TESTINGENVIRONMENTLIST.getCode(), TESTINGENVIRONMENTLIST.getMsg());
            } catch (MainExection mainExection) {
                mainExection.printStackTrace();
            }
            integerArrayList.add(integer);
        });
        //先查询集合中的测试环境数据是否关联了接口 关联了不能删除
        boolean b = removeByIds(testingEnvironmentDeleteFrom.getIds());
        return b;
    }
}
